package com.daka.service.impl;

import com.daka.dao.VolunteerDao;
import com.daka.dao1.ActivitySignDao;
import com.daka.dao1.MemberDao;
import com.daka.entity.Member;
import com.daka.entity.MemberFinshInfo;
import com.daka.entity.User;
import com.daka.entity.Volunteer;
import com.daka.service.UserService;
import com.daka.service.VolunteerService;
import com.daka.tools.ConfigUtil;
import com.daka.tools.DKQConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangx
 * Date: 2016/7/25 0025
 * Time: 11:24
 */
@Service("volunteerService")
@Slf4j
public class VolunteerServiceImpl implements VolunteerService {

    @Resource
    private VolunteerDao volunteerDao;

    @Resource
    private MemberDao memberDao;

    @Resource
    private ActivitySignDao activitySignDao;

    @Resource
    private UserService userService;

    public void migrate(Integer max, Integer min) {

        Integer seed = 10000;

        List<Member> memberList;
        if (max == 0 || min == 0 || max - min < seed) {
            memberList = memberDao.selectAll();

            insertBatch(memberList);
        } else {
            int index = (max - min) / seed;

            for (int i = 1; i <= index + 1; i++) {

                int pMin = (seed * (i - 1)) + min;
                int pMax = (seed * (i - 1)) + seed + min - 1;
                log.info("Volunteer insert 第{" + i + "}批，" + pMin + " 到" + pMax + "条");

                memberList = memberDao.selectByIdBetween(pMax, pMin);

                insertBatch(memberList);
            }
        }
    }

    private int insertBatch(List<Member> memberList) {
        List<Volunteer> volunteerList = new ArrayList<Volunteer>();

        for (Member member : memberList) {

            log.info("=============vol insertBatch============== member : " + member.getId());

            User user = userService.findByOldId(member.getId().longValue());

            List<Integer> actList = activitySignDao.selectUserActs(member.getId(), DKQConstant.DEL_NO);

            String lastActivityTime = activitySignDao.selectUserLast(member.getId(), DKQConstant.DEL_NO);

//            MemberFinshInfo memberFinshInfo = memberFinshInfoDao.selectByMember(member.getMemberid());
            MemberFinshInfo memberFinshInfo = null;

            if (null == user) {
                log.info("user not exists id : " + member.getId());
            } else {
                volunteerList.add(Volunteer.build(member, user, actList.size(), lastActivityTime, memberFinshInfo == null ? DKQConstant.DEL_NO : DKQConstant.DEL_YES));
            }

            if (volunteerList.size() >= ConfigUtil.BATCH_INSERT_MAX) {
                int i = volunteerDao.insertBatch(volunteerList);

                if (i > 0) {
                    volunteerList.clear();
                }
            }

        }

        if (volunteerList.size() > 0) {
            return volunteerDao.insertBatch(volunteerList);
        }

        return 0;
    }

    public void correction() {
        List<String> cardNoList = volunteerDao.selectByCardNoGroup();
        for (String cardNo : cardNoList) {
            log.info("============vol correction==========> cardNo : " + cardNo);
            List<Volunteer> volunteerList = volunteerDao.selectByCardNo(cardNo);

            if(null == cardNo || cardNo.length() < 15){
                for (Volunteer volunteer : volunteerList){
                    log.info("===========vol correction===========> volunteer : " + volunteer.getId());
                    volunteer.setCardno(volunteer.getId()+"");

                    volunteerDao.updateByPrimaryKeySelective(volunteer);
                }
            }else {
                for (int i = 0 ; i < volunteerList.size();i++){

                    Volunteer volunteer = volunteerList.get(i);
                    log.info("===========vol correction===========> volunteer : " + volunteer.getId());
                    volunteer.setCardno(volunteer.getCardno()+i);
                    volunteerDao.updateByPrimaryKeySelective(volunteer);
                }
            }
        }
    }
}
